
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/* MenuDemo.java requires images/middle.gif. */

/*
 * This class is just like MenuLookDemo, except the menu items
 * actually do something, thanks to event listeners.
 */
public class menu implements ActionListener, ItemListener {
    JTextArea output;
    JScrollPane scrollPane;
    String newline = "\n";

    public JMenuBar createMenuBar() {
        final JMenuBar menuBar;
        JMenu file, edit, view, help, submenu;
        JMenuItem newFile, open, save, exit, undo, redo, copy, cut, paste, about;
        JRadioButtonMenuItem rbMenuItem;
        JCheckBoxMenuItem cbMenuItem;
        final JFileChooser fc = new JFileChooser();

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        file.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(file);
        
        edit = new JMenu("Edit");
        edit.setMnemonic(KeyEvent.VK_E);
        edit.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(edit);
        
        view = new JMenu("View");
        view.setMnemonic(KeyEvent.VK_V);
        view.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(view);
        
        help = new JMenu("Help");
        help.setMnemonic(KeyEvent.VK_H);
        help.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(help);

        /*File Menu Operations*/ {
        
        //a group of JMenuItems
        newFile = new JMenuItem("New",
                                 KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        newFile.setMnemonic(KeyEvent.VK_N);
        newFile.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        newFile.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
        newFile.addActionListener(this);
        file.add(newFile);
        
        open = new JMenuItem("Open File...",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        open.setMnemonic(KeyEvent.VK_O);
        open.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        open.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        open.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent evt) {
        		fc.showOpenDialog(menuBar);
        	}
        });
        file.add(open);
        
        save = new JMenuItem("Save",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        save.setMnemonic(KeyEvent.VK_S);
        save.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        save.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        save.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent evt) {
        		fc.showSaveDialog(menuBar);
        	}
        });
        file.add(save);
        
        exit = new JMenuItem("Exit",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        exit.setMnemonic(KeyEvent.VK_X);
        //exit.setAccelerator(KeyStroke.getKeyStroke(
        		//KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        exit.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        exit.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent evt)
        	{
        		System.exit(0);
        	}
        });
        file.add(exit);
        
        }
        
        /*Edit Menu Operations*/ {
        
        undo = new JMenuItem("Undo",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        undo.setMnemonic(KeyEvent.VK_U);
        undo.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
        undo.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        undo.addActionListener(this);
        edit.add(undo);
        
        redo = new JMenuItem("Redo",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        redo.setMnemonic(KeyEvent.VK_R);
        redo.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
        redo.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        redo.addActionListener(this);
        edit.add(redo);
        
        cut = new JMenuItem("Cut",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        cut.setMnemonic(KeyEvent.VK_T);
        cut.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_X, ActionEvent.CTRL_MASK));
        cut.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        cut.addActionListener(this);
        edit.add(cut);
        
        copy = new JMenuItem("Copy",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        copy.setMnemonic(KeyEvent.VK_C);
        copy.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_C, ActionEvent.CTRL_MASK));
        copy.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        copy.addActionListener(this);
        edit.add(copy);
        
        paste = new JMenuItem("Paste",
                KeyEvent.VK_T);
        //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
        paste.setMnemonic(KeyEvent.VK_P);
        paste.setAccelerator(KeyStroke.getKeyStroke(
        		KeyEvent.VK_P, ActionEvent.CTRL_MASK));
        paste.getAccessibleContext().setAccessibleDescription(
        		"This doesn't really do anything");
        paste.addActionListener(this);
        edit.add(paste);
        
        }
        
        /*Help Menu Operations*/ {
        	
        	about = new JMenuItem("About",
                    KeyEvent.VK_T);
            //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
            about.setMnemonic(KeyEvent.VK_A);
            //undo.setAccelerator(KeyStroke.getKeyStroke(
            	//	KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
            about.getAccessibleContext().setAccessibleDescription(
            		"This doesn't really do anything");
            about.addActionListener(this);
            help.add(about);        	
        	
        }
/*
        ImageIcon icon = createImageIcon("images/middle.gif");
        menuItem = new JMenuItem("Both text and icon", icon);
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(this);
        file.add(menuItem);

        menuItem = new JMenuItem(icon);
        menuItem.setMnemonic(KeyEvent.VK_D);
        menuItem.addActionListener(this);
        file.add(menuItem);

        //a group of radio button menu items
        file.addSeparator();
        ButtonGroup group = new ButtonGroup();

        rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
        rbMenuItem.setSelected(true);
        rbMenuItem.setMnemonic(KeyEvent.VK_R);
        group.add(rbMenuItem);
        rbMenuItem.addActionListener(this);
        file.add(rbMenuItem);

        rbMenuItem = new JRadioButtonMenuItem("Another one");
        rbMenuItem.setMnemonic(KeyEvent.VK_O);
        group.add(rbMenuItem);
        rbMenuItem.addActionListener(this);
        file.add(rbMenuItem);

        //a group of check box menu items
        file.addSeparator();
        cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
        cbMenuItem.setMnemonic(KeyEvent.VK_C);
        cbMenuItem.addItemListener(this);
        file.add(cbMenuItem);

        cbMenuItem = new JCheckBoxMenuItem("Another one");
        cbMenuItem.setMnemonic(KeyEvent.VK_H);
        cbMenuItem.addItemListener(this);
        file.add(cbMenuItem);

        //a submenu
        file.addSeparator();
        submenu = new JMenu("A submenu");
        submenu.setMnemonic(KeyEvent.VK_S);

        menuItem = new JMenuItem("An item in the submenu");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_2, ActionEvent.ALT_MASK));
        menuItem.addActionListener(this);
        submenu.add(menuItem);

        menuItem = new JMenuItem("Another item");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        file.add(submenu);
*/
        //Build second menu in the menu bar.

        return menuBar;
    }

    public Container createContentPane() {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);

        //Create a scrolled text area.
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);

        //Add the text area to the content pane.
        contentPane.add(scrollPane, BorderLayout.CENTER);

        return contentPane;
    }

    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        String s = "Action event detected."
                   + newline
                   + "    Event source: " + source.getText()
                   + " (an instance of " + getClassName(source) + ")";
        output.append(s + newline);
        output.setCaretPosition(output.getDocument().getLength());
    }

    public void itemStateChanged(ItemEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        String s = "Item event detected."
                   + newline
                   + "    Event source: " + source.getText()
                   + " (an instance of " + getClassName(source) + ")"
                   + newline
                   + "    New state: "
                   + ((e.getStateChange() == ItemEvent.SELECTED) ?
                     "selected":"unselected");
        output.append(s + newline);
        output.setCaretPosition(output.getDocument().getLength());
    }

    // Returns just the class name -- no package info.
    protected String getClassName(Object o) {
        String classString = o.getClass().getName();
        int dotIndex = classString.lastIndexOf(".");
        return classString.substring(dotIndex+1);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = menu.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("MenuDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        menu demo = new menu();
        frame.setJMenuBar(demo.createMenuBar());
        frame.setContentPane(demo.createContentPane());

        //Display the window.
        frame.setSize(450, 260);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
